'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview boss岛
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var rivalService = require('../../../services/rivalService');
var heroAttr = require('../../../libs/war/heroAttr');
var genRewardService = require('../../../services/genRewardService');
var rewardService = require('../../../services/rewardService');
var dataApi = require('../../../libs/dataApi');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * boss战斗
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.fight = function (msg, session, next) {
    var role_id = session.uid;
    var road_id = msg.road_id;

    if (!road_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var atk_hero_attr;
    var def_hero_attr;
    var reward_list = [];
    var pvp_model;
    var role_model;
    var player_manager = new PlayerManager(role_id);
    var def_player_manager;
    var boss_type;
    var is_success;
    var road_boss;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['pvp', 'role', 'activity', 'bag', 'build', 'soldier', 'skill', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                pvp_model = player_manager.pvp;
                role_model = player_manager.role;
                road_boss = pvp_model.getRoadBoss(road_id);
                if (!road_boss) {
                    return next(null, {
                        code: Code.BATTLE_BOSS_NOT_EXIST
                    });
                }
                boss_type = road_boss.boss_type;
                atk_hero_attr = heroAttr.getAttr(player_manager.role, player_manager.bag, player_manager.skill, player_manager.build);
                rivalService.getRandomRivalRoleId(role_id, [], cb);
            },
            function (rival_role_id, cb) {
                if (!rival_role_id) {
                    var boss_reward_conf = dataApi.pvp_boss_reward.findById(boss_type);
                    if (!!boss_reward_conf) {
                        _.each(boss_reward_conf, function (boss_reward) {
                            if (boss_reward.job_type == role_model.job_type) {
                                reward_list.push(genRewardService.generateReward(boss_reward));
                            }
                        });
                        rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_WORLD_BOSS, operationType.OPR_WORLD_BOSS_FIGHT);
                    }
                    return next(null, {
                        code: Code.OK,
                        result: {
                            is_success: true,
                            reward_list: reward_list
                        }
                    });
                }
                def_player_manager = new PlayerManager(rival_role_id);
                cb();
            },
            function (cb) {
                def_player_manager.refreshData(cb);
            },
            function (cb) {
                def_player_manager.getModel(
                    ['role', 'bag', 'skill', 'build', 'soldier', 'accrued'],
                    cb
                );
            },
            function (cb) {
                def_hero_attr = heroAttr.getAttr(def_player_manager.role, def_player_manager.bag, def_player_manager.skill, def_player_manager.build);
                var atk_value = atk_hero_attr.attr.min_atk + atk_hero_attr.attr.hp / 20 + atk_hero_attr.command / 1.5;
                var def_value = def_hero_attr.attr.min_atk + def_hero_attr.attr.hp / 20 + def_hero_attr.command / 1.5;
                is_success = false;
                var success_rate = atk_value / def_value;
                if (success_rate > 1.2) {
                    is_success = true;
                } else if (success_rate < 0.8) {
                    is_success = false;
                } else {
                    success_rate *= 0.6;
                    if (_.random(0, 1) > success_rate) {
                        is_success = true;
                    }
                }
                if (is_success) {
                    var boss_reward_conf = dataApi.pvp_boss_reward.findById(boss_type);
                    if (!!boss_reward_conf) {
                        _.each(boss_reward_conf, function (pvp_boss_item) {
                            if (pvp_boss_item.job_type === role_model.job_type) {
                                reward_list.push(genRewardService.generateReward(pvp_boss_item));
                            }
                        });
                        rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_WORLD_BOSS, operationType.OPR_WORLD_BOSS_FIGHT);
                    }

                    pvp_model.removeRoadBoss(road_boss);
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    is_success: is_success,
                    reward_list: reward_list
                }
            });
        }
    );
};
